// 节流
export function throttle(fn: { apply: (arg0: any, arg1: any[]) => void }, t: number) {
  let flag = true;
  const interval = t || 500;
  return function (this: any, ...args: any) {
      if (flag) {
          fn.apply(this, args);
          flag = false;
          setTimeout(() => {
              flag = true;
          }, interval);
      }
  };
}

// 防抖
export function debounce(fn: { apply: (arg0: any, arg1: any) => void }, t: number) {
  let timeId: any = null;
  const delay = t || 500;
  return function (this: any, ...args: any) {
      if (timeId) {
          clearTimeout(timeId);
      }
      timeId = setTimeout(() => {
          timeId = null;
          fn.apply(this, args);
      }, delay);
  };
}
const sprintf= (num:number, len = 2) => (`0${num}`).slice(-len)

export function formatTime(timestamp:number){
  
    // const hour = sprintf( Math.floor(timestamp/3600) ); // 获取小时
    const minute = sprintf(Math.floor(timestamp%3600/60)); // 获取分钟
    const second = sprintf(Math.floor(timestamp%60)); 
    return minute+':'+second
}

export const createRandomChinese = (count:number) => {
    const start = parseInt('4e00', 16)
    const end = parseInt('9fa5', 16)
    let name = ''
    for(let i = 0; i < count; i++) {
        const cha = Math.floor(Math.random() * (end - start))
        name += '\\u' + (start + cha).toString(16)
    }
    return eval(`'${name}'`)
}
